package com.the_ring.chapter09.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;

import com.the_ring.chapter09.R;
import com.the_ring.chapter09.adapter.GoodsAdapter;
import com.the_ring.chapter09.bean.ItemInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class GoodsFragment extends Fragment {

    public static final int APPLIANCE = 1;
    public static final int COSTUME = 0;

    private Context context;
    private GoodsAdapter adapter;

    private int tag;


    public GoodsFragment(Context context, int tag) {
        // Required empty public constructor
        this.context = context;
        this.tag = tag;
    }

    public static GoodsFragment newInstance(Context context, int tag) {
        GoodsFragment fragment = new GoodsFragment(context, tag);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_goods, container, false);

        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.srl_goods);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(() -> {
                    refresh();
                    adapter.notifyDataSetChanged();
                    refreshLayout.setRefreshing(false);
                }, 300);
            }
        });

        adapter = new GoodsAdapter(context, infoList);
        RecyclerView goodsRl = view.findViewById(R.id.rl_goods);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        goodsRl.setLayoutManager(layoutManager);
        goodsRl.setAdapter(adapter);


        return view;
    }

    private List<ItemInfo> infoList;
    private int[] resIds;

    private String[] title = new String[]{"双十一", "大恩惠", "爆款价", "快点击", "手慢无", "就一次"};

    private void initData() {
        if (tag == APPLIANCE) {
            resIds = new int[]{R.drawable.appliance_0, R.drawable.appliance_1, R.drawable.appliance_2, R.drawable.appliance_3, R.drawable.appliance_4, R.drawable.appliance_5, R.drawable.appliance_6, R.drawable.appliance_7, R.drawable.appliance_8};
        } else if (tag == COSTUME) {
            resIds = new int[]{R.drawable.costume_0, R.drawable.costume_1, R.drawable.costume_2, R.drawable.costume_3, R.drawable.costume_4, R.drawable.costume_5, R.drawable.costume_6, R.drawable.costume_7, R.drawable.costume_8};
        }

        infoList = new ArrayList<>();
        infoList.add(new ItemInfo(resIds[0], "双十一"));
        infoList.add(new ItemInfo(resIds[1], "大恩惠"));
        infoList.add(new ItemInfo(resIds[2], "惊爆价"));
        infoList.add(new ItemInfo(resIds[3], "快点击"));
        infoList.add(new ItemInfo(resIds[4], "手慢无"));
        infoList.add(new ItemInfo(resIds[5], "就一次"));
    }

    private void refresh() {
        Random random = new Random();
        infoList.add(0, new ItemInfo(resIds[random.nextInt(resIds.length)], title[random.nextInt(title.length)]));
    }
}